//
//  RecommendVC.m
//  BiliBiliDemo
//
//  Created by XMG on 16/4/20.
//  Copyright © 2016年 XDH. All rights reserved.
//

#import "RecommendVC.h"
#import "RecommendCell.h"
#import "HeadView.h"
#import "FooterView.h"

static NSString * const ID = @"cell";
static NSString * const kheaderID = @"headView";
static NSString * const kfooterID = @"footView";

@interface RecommendVC () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end

@implementation RecommendVC

- (void)loadView
{
    //设置布局方式
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    //flow的属性 实质是通过代理方法实现
    flow.itemSize = CGSizeMake(SCREEN_WIDTH * 0.5, 100);
    flow.minimumInteritemSpacing = 0;//每组中的最小item间距
    flow.minimumLineSpacing = 0;//每组中的最小行间距
    
    //设置collectionView
    UICollectionView *collectView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flow];
    self.view = collectView;
    //注册headerView
    [collectView registerNib:[UINib nibWithNibName:@"HeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderID];
    //注册footerView
    [collectView registerNib:[UINib nibWithNibName:@"FooterView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterID];
    
    //设置代理
    collectView.dataSource = self;
    collectView.delegate = self;
    //注册cell
    [collectView registerNib:[UINib nibWithNibName:@"RecommendCell" bundle:nil] forCellWithReuseIdentifier:ID];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 5;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.image = [UIImage imageWithColor:[UIColor orangeColor]];
    
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    //设置headerView
    if (kind == UICollectionElementKindSectionHeader) {
        HeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderID forIndexPath:indexPath];
        
        reusableview = headerView;
    }
    //设置footerView
    if (kind == UICollectionElementKindSectionFooter) {
        HeadView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterID forIndexPath:indexPath];
        reusableview = view;
    }
    return reusableview;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        CGSize size = {0, 150};
        return size;
    }
    else
    {
        CGSize size = {0, 44};
        return size;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    CGSize size = {0, 64};
    return size;
}

@end
